Skip to content

fix(sqlite): Convert ORDER BY and LIMIT ... OFFSET clauses in DELETE/UPDATE statements into shared AST nodes - #4707

Open
manuelschoene wants to merge 3 commits into
sqlc-dev:mainfrom
manuelschoene:fix/sqlite-delete-update-orderby-limit
Open

manuelschoene wants to merge 3 commits into
sqlc-dev:mainfrom
manuelschoene:fix/sqlite-delete-update-orderby-limit

Conversation

@manuelschoene

Copy link
Copy Markdown

SQLite accepts ORDER BY and LIMIT ... OFFSET on UPDATE and DELETE (with SQLITE_ENABLE_UPDATE_DELETE_LIMIT), and sqlc parses that syntax on purpose. The meyer parser fills OrderBy and Limit.Offset on DeleteStmt / UpdateStmt, but the conversion into sqlc's AST kept only Limit.Count. Any parameter in the ORDER BY or OFFSET was therefore missing from the generated method, which then failed at runtime with too few arguments. On the core analyzer path the LIMIT parameter was dropped as well.

Fixes #4706.

Changes

  • AST: ast.DeleteStmt and ast.UpdateStmt gain SortClause and LimitOffset (same names as on SelectStmt), and Format prints ORDER BY ... LIMIT ... OFFSET ....
  • astutils: Walk and Apply visit the new fields. Apply did not visit LimitCount on these two statements before; it does now.
  • SQLite converter: limitCount is replaced by convertOrderLimit, which converts OrderBy, Limit.Count and Limit.Offset.
  • Compiler: find_params.go tracks LimitOffset on DELETE / UPDATE, so the parameter is named Offset as it is for SELECT.
  • Core analyzer: analyzeUpdate / analyzeDelete now type the sort clause and the limit/offset (typeOrderLimit).

Tests

New end-to-end case internal/endtoend/testdata/update_delete_order_by_limit/sqlite with four queries: DELETE and UPDATE, each once with LIMIT ? OFFSET ? and once with a parameter in ORDER BY. Its golden file was written before the fix, by hand, with the names and types sqlc generates for the equivalent SELECT. It failed before the fix and passes after it without changes.

  • TestReplay, TestFormat and TestJsonSchema pass. Database-backed tests were not run locally.
  • Core context (SQLC_TEST_CORE=1): the parameters are now bound, but LIMIT / OFFSET parameters are named Column3 / Column4 there, so the new case fails in that context. Plain SELECT ... LIMIT ? OFFSET ? has the same naming on the core path (limit/sqlite and select_limit/sqlite already fail there on main). The set of failing core cases is identical before and after this change.

Not in this PR

  • UpdateStmt / DeleteStmt Format still prints RETURNING after LIMIT, while SQLite expects it before ORDER BY / LIMIT. This predates the change.
  • MySQL also allows ORDER BY ... LIMIT on UPDATE / DELETE. The dolphin converter only sets LimitCount and could use the new fields. Not the topic of this PR.

The CI uses a SQLite database built without the  SQLITE_ENABLE_UPDATE_DELETE_LIMIT option enabled. Therefore, the syntax is not recognized and is expected to fail.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlite: Missing parameters in ORDER BY and OFFSET clauses inside DELETE/UPDATE statements

1 participant